<template>
  <el-dialog
  :title="title"
  :visible.sync="visible"
  width="40%"
  :before-close="handleClose">
    <el-form :model="formData" :rules="rules" ref="formData" label-width="100px" class="demo-ruleForm" style="width:90%">
    <el-form-item label="广告图片：" prop="imageUrl">
        <el-upload
            class="avatar-uploader"
            action=""
            :show-file-list="false"
            :http-request="uploadMainImg">
            <img v-if="formData.imageUrl" :src="formData.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
    </el-form-item>    
    <el-form-item label="广告标题：" prop="title">
        <el-input v-model="formData.title"></el-input>
    </el-form-item>
    <el-form-item label="广告链接：" prop="advertUrl">
        <el-input v-model="formData.advertUrl"></el-input>
    </el-form-item>
    <el-form-item label="跳转方式：" prop="advertTarget">
        <el-select v-model="formData.advertTarget" clearable placeholder="请选择">
        <el-option label="新窗口打开" value="_blank"></el-option>
        <el-option label="当前窗口打开" value="_self"></el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="状态：" prop="status">
        <el-radio-group v-model="formData.status">
        <el-radio :label="1">正常</el-radio>
        <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="广告位置：" prop="position">
        <el-input-number v-model="formData.position" :min="1" :max="100"></el-input-number>
    </el-form-item>
    <el-form-item label="排序：" prop="sort">
        <el-input-number v-model="formData.sort" :min="1" :max="100"></el-input-number>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" size="mini" @click="submitForm('formData')">确定</el-button>
        <el-button @click="handleClose" size="mini">取消</el-button>
    </el-form-item>
    </el-form>
    </el-dialog>
</template>

<script>
import api from '@/api/advert'

export default {
    data() {
        return {
            rules: {
                imageUrl: [
                    { required: true, message: '请选择图片', trigger: 'blur' }
                    
                ],
                title: [
                    { required: true, message: '请输入广告标题', trigger: 'blur' }
                    
                ],
                advertUrl: [
                    { required: true, message: '请输入广告链接', trigger: 'blur' }
                    
                ],
                advertTarget: [
                    { required: true, message: '请选择跳转方式', trigger: 'change' },                   
                ],
                status: [
                    { required: true, message: '请选择状态', trigger: 'change' },                   
                ],
                position: [
                    { required: true, message: '请选择广告位置', trigger: 'change' },                   
                ],
                sort: [
                    { required: true, message: '请选择排序号', trigger: 'change' },                   
                ],
            }
        }
    },
    props:{
        visible:{
            type:Boolean,
            default:false
        },
        title:{
            type:String,
            default:''
        },
        formData:{
            type:Object,
            default:{}
        },
        remoteClose: Function ,
        oldImageUrl: {
            type:String,
            default:''
        },
    },
    methods: {
        // 关闭窗口
        handleClose(){
            this.$refs.formData.resetFields()
            this.remoteClose()
        },
        // 提交表格，验证
        submitForm(formName){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submitData()
                } else {
                    this.$message({
                    message: '保存失败',
                    type: 'error'
                    });
                    return false;
                }
            });
        },
        // 提交数据，检验
        async submitData(){
            let response=null
            if(this.formData.id){
                response = await api.update(this.formData)
            }else{
                response = await api.add(this.formData)
            }
            
            if (response.code === 20000){
                this.$message({
                    message: '保存成功',
                    type: 'success'
                    });
                this.handleClose()
                
            }else{
                this.$message({
                    message: '保存失败',
                    type: 'error'
                    });
            }
        },
        // 上传主图
        uploadMainImg(file){
            const data=new FormData()
            data.append('file',file.file)
            api.uploadImg(data).then(response=>{
                this.deleteImg()
                this.formData.imageUrl= response.data
            }).catch(
                ()=>{
                    this.$message.error('上传图片失败')
                }
            )
        },
        // 删除主图
        deleteImg(){
            if(this.formData.imageUrl && this.formData.imageUrl!==this.oldImageUrl){
                api.deleteImg(this.formData.imageUrl)
            }
            
        }

    },
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>